package com.samphanie.anecloud.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 跨域配置
 * @Author ZSY
 * @createTime 2021/3/25 9:54
 */
@Configuration
public class AneCloudCorsConfig {

    @Bean
    public CorsFilter corsWebFilter() {

        // 1.添加CORS配置信息
        CorsConfiguration config = new CorsConfiguration();
        // 允许跨越发送cookie
        config.setAllowCredentials(true);
        // 允许所有域名进行跨域调用
        //        if (environment.acceptsProfiles(Profiles.of("dev"))) {
        //            config.setAllowedOrigins(Collections.singletonList("http://localhost:9999"));
        //        } else {
        //            config.setAllowedOrigins(Collections.singletonList("https://uaa.imooc.com"));
        //        }
        config.addAllowedOrigin("*");
        // 放行全部原始头信息
        config.addAllowedHeader("*");
        // config.setAllowedHeaders(Collections.singletonList("*"));
        config.addExposedHeader("X-Authenticate");
        // 允许所有请求方法跨域调用
        config.addAllowedMethod("*");
        // config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS"));
        // 设置访问时间
        config.setMaxAge(3600L);

        // 2.添加映射路径，我们拦截一切请求
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);

        return new CorsFilter(source);
    }



}
